package com.wkbb.sports.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.wkbb.sports.dto.HlifeTeachCourseDto;
import com.wkbb.sports.dto.TrainDto;
import com.wkbb.sports.mapper.HlifeCourseMapper;
import com.wkbb.sports.model.HlifeTeachCourse;
import com.wkbb.sports.model.HlifeTeachPlan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.sports.dto.TeachDocumentDto;
import com.wkbb.sports.dto.TeachPlanDetailDto;
import com.wkbb.sports.dto.TeachPlanDto;
import com.wkbb.sports.mapper.HlifeTeachPlanDetailMapper;
import com.wkbb.sports.mapper.HlifeTeachPlanMapper;
import com.wkbb.sports.model.HlifeTeachPlanDetail;
import com.wkbb.sports.service.TeachPlanDetailService;
import com.wkbb.sports.utils.BeanBind;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TeachPlanDetailServiceImpl extends BaseServiceImpl<HlifeTeachPlanDetail> implements TeachPlanDetailService {

    @Autowired
    public void setMapper(HlifeTeachPlanDetailMapper hlifeTeachPlanDetailMapper) {
        super.setMapper(hlifeTeachPlanDetailMapper);
    }

    @Autowired
    HlifeTeachPlanDetailMapper hlifeTeachPlanDetailMapper;


    @Autowired
    HlifeTeachPlanMapper hlifeTeachPlanMapper;

    @Autowired
    private HlifeCourseMapper courseMapper;


    @Override
    public void publishPlanDetail(Long teachPlanDetailId) {

        hlifeTeachPlanDetailMapper.publisTeachPlan(teachPlanDetailId);

    }

    @Override
    public List<TeachPlanDetailDto> selClassPlanDetail(Map<String, Object> map) {
        List<TeachPlanDetailDto> list = hlifeTeachPlanDetailMapper.selClassPlanDetail(map);
        HlifeTeachCourse course = new HlifeTeachCourse();
        course.setClassId((Long) map.get("classId"));
        for (TeachPlanDetailDto dto : list) {
            course.setTeachPlanDetailId(dto.getId());
            HlifeTeachCourseDto courseDto = courseMapper.findCourse(course);
            if (courseDto != null) {
                courseDto.setPlanContent(dto.getPlanContent());
                dto.setCourseDto(courseDto);
            }
        }
        return list;
    }

    @Override
    public int selMaxCourseSeq(TeachPlanDetailDto teachPlanDetailDto) {

        return hlifeTeachPlanDetailMapper.selMaxCourseSeq(teachPlanDetailDto);
    }

    @Override
    public void addDocument(TeachDocumentDto teachDocumentDto) {

        hlifeTeachPlanDetailMapper.addDocument(teachDocumentDto);
    }

    @Override
    public List<TeachDocumentDto> selDocument(String documentIds) {

        return hlifeTeachPlanDetailMapper.findDocumentList(documentIds);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void courseTeachAgain(HlifeTeachPlanDetail hlifeTeachPlanDetail) {

        hlifeTeachPlanMapper.updCourseCount(hlifeTeachPlanDetail);

        hlifeTeachPlanDetailMapper.insert(hlifeTeachPlanDetail);
    }

    @Override
    public boolean bindDocuments(Long teachPlanDetailId, String documentIds) {
        HlifeTeachPlanDetail planDetail =  hlifeTeachPlanDetailMapper.getByPrimaryKey(teachPlanDetailId);
        if (planDetail != null){
            List<TeachDocumentDto> documentDtos = hlifeTeachPlanDetailMapper.findDocumentList(documentIds);
            if (!documentDtos.isEmpty()){
                StringBuilder names = new StringBuilder();
                StringBuilder ids = new StringBuilder();
                for (TeachDocumentDto documentDto : documentDtos){
                    names.append(documentDto.getOutline()).append(",");
                    ids.append(documentDto.getId()).append(",");
                }
                if (names.length() > 0){
                    names.deleteCharAt(names.length() - 1);
                    ids.deleteCharAt(ids.length() - 1);
                }
                planDetail.setDocumentIds(ids.toString());
                if ("".equals(planDetail.getPlanContent()) || planDetail.getPlanContent() == null){
                    planDetail.setPlanContent(names.toString());
                }
                return hlifeTeachPlanDetailMapper.updatePlanContent(planDetail) > 0;
            }
        }
        return false;
    }

    @Override
    public boolean bindFiles(Long teachPlanDetailId, String fileIds) {
        HlifeTeachPlanDetail planDetail =  hlifeTeachPlanDetailMapper.getByPrimaryKey(teachPlanDetailId);
        if (planDetail != null){
            planDetail.setFileIds(fileIds);
            return hlifeTeachPlanDetailMapper.updatePlanContent(planDetail) > 0;
        }
        return false;
    }

	@Override
	public PageInfo<TeachDocumentDto> selTeachDocumentList(Map<String, Object> params, int pageNo, int pageSize)
			throws Exception {
		
		Page<TeachDocumentDto> page = PageHelper.startPage(pageNo, pageSize, true);
		
		List<TeachDocumentDto> docs = hlifeTeachPlanDetailMapper.getDocumentByMapParams(params);
		
        PageInfo<TeachDocumentDto> pageInfo = new PageInfo<TeachDocumentDto>(docs);
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
	}

	@Override
	public PageInfo<TeachPlanDetailDto> selPlanDetail(Map<String, Object> params, int pageNo, int pageSize)
			throws Exception {
		Page<TeachPlanDetailDto> page = PageHelper.startPage(pageNo, pageSize, true);
		
		List<TeachPlanDetailDto> details = hlifeTeachPlanDetailMapper.findListByMapParams(params);
//		List<HlifeTeachPlanDetail> details = hlifeTeachPlanDetailMapper.getListByMapParams(params);

        PageInfo<TeachPlanDetailDto> pageInfo = new PageInfo<TeachPlanDetailDto>(details);
        
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
	}

    @Override
    public boolean delTeachDocument(Long id) {
        return hlifeTeachPlanDetailMapper.delTeachDocument(id) > 0;
    }

    @Override
	public void updDocument(TeachDocumentDto teachDocumentDto) {
		
		hlifeTeachPlanDetailMapper.updDocument(teachDocumentDto);
	}

	@Override
	public void updTeachPlanDetail(HlifeTeachPlanDetail hlifeTeachPlanDetail) {
		
		hlifeTeachPlanDetailMapper.update(hlifeTeachPlanDetail);
		
	}


}
